from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer


class SecretOauth(object):
    """加密解密"""

    def dumps(self, data):
        """加密"""
        # 1.根据 签名(secret_key) 创建序列化对象
        serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=24 * 15 * 60)
        # 2. 通过 dumps方法 加密数据
        result = serializer.dumps(data)
        # 3. result是bytes类型转换成 str
        return result.decode()

    def loads(self, data):
        """解密"""
        # 1.根据 签名(secret_key) 创建序列化对象
        serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=24 * 15 * 60)
        # 2. 通过 loads方法 解密数据
        result = serializer.loads(data)
        # 3. 返回解密完毕数据
        return result

